package com.ebring.shareEducation.controller;

import com.ebring.shareEducation.bean.*;
import com.ebring.shareEducation.bean.response.GoldCoinScore;
import com.ebring.shareEducation.bean.response.UserReleaseReviews;
import com.ebring.shareEducation.bean.response.UserTlent;
import com.ebring.shareEducation.dao.*;
import com.ebring.shareEducation.bean.ScoreOrder;
import com.ebring.shareEducation.bean.Talent;
import com.ebring.shareEducation.bean.Taskrelease;
import com.ebring.shareEducation.bean.UserReviews;
import com.ebring.shareEducation.dao.ScoreOrderMapper;
import com.ebring.shareEducation.dao.TalentMapper;
import com.ebring.shareEducation.dao.TaskreleaseMapper;
import com.ebring.shareEducation.dao.UserReviewsMapper;
import com.ebring.shareEducation.service.UserReviewsService;
import com.ebring.shareEducation.service.UserService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UserReviewsController {

    @Autowired
    private UserReviewsService userReviewsService;

    @Autowired
    private UserReviewsMapper userReviewsMapper;


    @Autowired
    private UserMapper userMapper;


    @Autowired
    private TalentMapper talentMapper;


    @Autowired
    private UserService userService;

    @Autowired
    private ScoreOrderMapper scoreOrderMapper;

    @Autowired
    private TaskreleaseMapper taskreleaseMapper;


    /**
     * 根据用户id查看用户评论
     *
     *
     * @return
     */
    @PostMapping("/userReviews/selectTaskReviews")
    public RspMsg<UserReviews> selectTaskReviews(UserReviews userReviews) {
        return userReviewsService.selectTaskReviews(userReviews);
    }

    /**
     * 根据任务id查看用户评论
     *
     *
     * @return
     */
    @PostMapping("/userReviews/selectUserReviews")
    public RspMsg<UserReviews> selectUserReviews(UserReviews userReviews) {
        return userReviewsService.selectUserReviews(userReviews);
    }

    /**
     * 评论
     * @param userReviews
     * @return
     */
    @PostMapping("/userReviews/insertreview")
    @ResponseBody
    public RspMsg<UserReviews> insertUserReviews(UserReviews userReviews,String vipType) {

        return    userReviewsService.insertUserReviews(userReviews,vipType);
    }

    /**
     * 用户签到
     * @param user
     * @return
     */
    @PostMapping("/user/UserSign")
    @ResponseBody
    public RspMsg<User>  UserSign (User user,String vipType){
        return  userReviewsService.UserSign(user ,vipType);
    }

    /**
     * 用户签到金币积分查询
     * @param
     * @return
     */
    @PostMapping("/user/selectUserSign")
    @ResponseBody
    public RspMsg<GoldCoinScore>  SelectUserSign (String userId){
        return  userReviewsService.SelectUserSign(userId);


    }


    /**
     * 用户信息查询：role=01 当前登录用户为雇主，雇主查看老师信息，并查询老师的评论信息
     *               role=02 当前登录用户为老师，老师查看家长信息，并查询家长的评论信息
     *               type=01 差评   type=02 中评   type=03 好评   其他默认为是查全部
     * @return
     */
    @PostMapping("/user/selectUserTalentByIdUser")
    @ResponseBody
    public RspMsg<UserTlent> selectUserTalentByIdUser(String userId ,String type,String role ) {
        RspMsg rspMsg = new RspMsg();
        UserTlent userTlent = new UserTlent();
        User user = userMapper.selectByPrimaryKey(userId);
        Example example = new Example(Talent.class);
        example.or().andEqualTo("userId",userId);
        List<Talent> talentList = talentMapper.selectByExample(example);
        Talent talents = new Talent();
        if(talentList.size()>0){
            talents = talentList.get(0);
        }
        userTlent.setUsername(user.getUsername());
        userTlent.setRealName(user.getRealName());
        userTlent.setSex(user.getSex());
        userTlent.setBirthday(user.getBirthday());
        userTlent.setGraduateSchool(user.getGraduateSchool());
        userTlent.setPhone(user.getPhone());
        userTlent.setEmail(user.getEmail());
        userTlent.setIdploma(user.getIdploma());
        userTlent.setIndustryName(talents.getIndustryId());
        userTlent.setSkillsName(talents.getSkillsName());
        userTlent.setWorkExperienc(user.getWorkExperienc());
        userTlent.setImagePath(user.getImagePath());
        userTlent.setPath1(talents.getPath1());
        userTlent.setPath2(talents.getPath2());
        List<UserReleaseReviews> userReleaseReviewsList;
        List<UserReleaseReviews> userReleaseReviewsList1;
        List<UserReviews> userReviewsList;
        List<UserReviews> userReviewsList1;
        List<UserReviews> userReviewsList2;
        if (role.equals("01")){   //用户信息查询：当前登录用户为雇主，雇主查看老师信息，并查询老师的评论信息 :01
            userReleaseReviewsList1 =  userReviewsMapper.selectReleaseUsers(userId,0,0);//所有评论
            if (type.equals("03")){
                userReleaseReviewsList =  userReviewsMapper.selectReleaseUsers(userId,1,2);//差评
            }else if(type.equals("02")){
                userReleaseReviewsList =  userReviewsMapper.selectReleaseUsers(userId,3,6);//中评
            }else if(type.equals("01")){
                userReleaseReviewsList =  userReviewsMapper.selectReleaseUsers(userId,7,10);//好评
            }else{
                userReleaseReviewsList =  userReviewsMapper.selectReleaseUsers(userId,0,0);//所有评论
            }
            //差评总数
            userReviewsList = userReviewsMapper.selectavgsss(userId, "01", 1, 2);//差评
            //中评总数
            userReviewsList1 = userReviewsMapper.selectavgsss(userId, "01", 3, 6);//中评

            userReviewsList2 = userReviewsMapper.selectavgsss(userId, "01", 7, 10);//好评

        }else{//用户信息查询：当前登录用户为老师，老师查看家长信息，并查询家长的评论信息: 02
            userReleaseReviewsList1 =  userReviewsMapper.selectUserTalentByIdUsers(userId,0,0);//所有评论
            if (type.equals("03")){
                userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,1,2);//差评
            }else if(type.equals("02")){
                userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,3,6);//中评
            }else if(type.equals("01")){
                userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,7,10);//好评
            }else{
                userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,0,0);//所有评论
            }
            //差评总数
            userReviewsList = userReviewsMapper.selectavgsss(userId, "02", 1, 2);//差评
            //中评总数
            userReviewsList1 = userReviewsMapper.selectavgsss(userId, "02", 3, 6);//中评
            //好评
            userReviewsList2 = userReviewsMapper.selectavgsss(userId, "02", 7, 10);//好评

        }

        Double avg = userReviewsMapper.selectavg(userId ,role);//评分平均值
        userTlent.setFavourableComment(userReviewsList2.size()+"");//好评
        userTlent.setChineseComment(userReviewsList1.size()+"");//中评总数
        userTlent.setNegativeComment(userReviewsList.size()+"");//差评总数
        userTlent.setGeneralComment(userReleaseReviewsList1.size()+"");//评论总数
        userTlent.setUserReleaseReviewsList(userReleaseReviewsList);//评论信息
        userTlent.setAvg(avg+"");//平均评分值
        if (userReleaseReviewsList.size()<0){
            rspMsg.setMsg("暂无评论");
        }
        rspMsg.setOK();
        rspMsg.setResult(userTlent);
        return rspMsg;
    }



    /**
     * 根据任务id 查询家个人信息评论信息
     * @return
     */
   @PostMapping("/user/selectUserTalentByIdUsers")
    @ResponseBody
    public RspMsg<UserTlent> selectUserTalentByIdUsers(String taskReleaseId ,String type) {
       Taskrelease taskrelease = taskreleaseMapper.selectByPrimaryKey(new Taskrelease().setTaskReleaseId(taskReleaseId));
       String userId = taskrelease.getUserId();
       RspMsg rspMsg = new RspMsg();
       UserTlent userTlent = new UserTlent();
       User user = userMapper.selectByPrimaryKey(userId);
       Example example = new Example(Talent.class);
       example.or().andEqualTo("userId",userId);
       List<Talent> talentList = talentMapper.selectByExample(example);
       Talent talents = new Talent();
       if(talentList.size()>0){
           talents = talentList.get(0);
       }
       userTlent.setUsername(user.getUsername());
       userTlent.setRealName(user.getRealName());
       userTlent.setSex(user.getSex());
       userTlent.setBirthday(user.getBirthday());
       userTlent.setGraduateSchool(user.getGraduateSchool());
       userTlent.setPhone(user.getPhone());
       userTlent.setEmail(user.getEmail());
       userTlent.setIdploma(user.getIdploma());
       userTlent.setIndustryName(talents.getIndustryId());
       userTlent.setSkillsName(talents.getSkillsName());
       userTlent.setWorkExperienc(user.getWorkExperienc());
       userTlent.setImagePath(user.getImagePath());
       userTlent.setPath1(talents.getPath1());
       userTlent.setPath2(talents.getPath2());
       List<UserReleaseReviews> userReleaseReviewsList;
       List<UserReleaseReviews> userReleaseReviewsList1;
       List<UserReviews> userReviewsList;
       List<UserReviews> userReviewsList1;
       List<UserReviews> userReviewsList2;
        //用户信息查询：当前登录用户为老师，老师查看家长信息
           userReleaseReviewsList1 =  userReviewsMapper.selectUserTalentByIdUsers(userId,0,0);//所有评论
           if (type.equals("03")){
               userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,1,2);//差评
           }else if(type.equals("02")){
               userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,3,6);//中评
           }else if(type.equals("01")){
               userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,7,10);//好评
           }else{
               userReleaseReviewsList =  userReviewsMapper.selectUserTalentByIdUsers(userId,0,0);//所有评论
           }
           //差评总数
           Example examples = new Example(UserReviews.class);
           examples.or().andBetween("avg",new String("1"),new String("2")).andEqualTo("reviewId",userId).andEqualTo("role","02");//1-2差评  3-6中评  7-10好评
           userReviewsList = userReviewsMapper.selectByExample(examples);
           //中评总数
           examples.or().andBetween("avg",new String("3"),new String("6")).andEqualTo("reviewId",userId).andEqualTo("role","02");
           userReviewsList1 = userReviewsMapper.selectByExample(examples);
           //好评
           examples.or().andBetween("avg",new String("7"),new String("10")).andEqualTo("reviewId",userId).andEqualTo("role","02");
           userReviewsList2 = userReviewsMapper.selectByExample(examples);
       Double avg = userReviewsMapper.selectavg(userId ,"02");//评分平均值
       userTlent.setFavourableComment(userReviewsList2.size()+"");//好评
       userTlent.setChineseComment(userReviewsList1.size()+"");//中评总数
       userTlent.setNegativeComment(userReviewsList.size()+"");//差评总数
       userTlent.setGeneralComment(userReleaseReviewsList1.size()+"");//评论总数
       userTlent.setUserReleaseReviewsList(userReleaseReviewsList);//评论信息
       userTlent.setAvg(avg+"");//平均评分值
       if (userReleaseReviewsList.size()<0){
           rspMsg.setMsg("暂无评论");
       }
       rspMsg.setOK();
       rspMsg.setResult(userTlent);
       return rspMsg;
    }


    /**
     * 根据评论差评好评进行查询
     * @param
     * @return
     */
    @PostMapping("/userReviews/SelectReviewsGrade")
    @ResponseBody
    public RspMsg<UserReviews> SelectReviewsGrade(String type ,String userId ,String role) {
        return userReviewsService.SelectReviewsGrade(type,userId ,role);
    }

    /**
     * 用户个人信息查询
     */
    @PostMapping("/userId/selectUser")
    @ResponseBody
    public RspMsg<User> selectUser(User user) {
        User user1 = userMapper.selectByPrimaryKey(user);
        RspMsg rspMsg = new RspMsg();
        rspMsg.setOK();
        rspMsg.setResult(user1);
        return rspMsg;
    }
    /**
     * 用户个人信息保存
     *
     * @return
     */
    @PostMapping("/user/PersonaInformation")
    @ResponseBody
    public RspMsg<User> PersonaInformation(User user,String vipType) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = formatter.format(new Date());//格式化数据
        RspMsg rspMsg = new RspMsg();
        User user1 = userMapper.selectByPrimaryKey(user.getUserId());
        int score =0;
        if (user.getSex()!=null && user1.getSex()==null){
            score = score+1;
        }
        if (user.getBirthday()!=null && user1.getBirthday()==null){
            score=score+1;
        }
        if (user.getIdploma()!=null && user1.getIdploma()==null){
            score=score+1;
        }
        if (user.getGraduateSchool()!=null && user1.getGraduateSchool()==null){
            score=score+1;
        }
        if (user.getWorkExperienc()!=null && user1.getWorkExperienc() ==null){
            score=score+1;
        }
        if(user.getProvinceName()!=null  && user1.getProvinceName()==null){
            score=score+1;
        }
        if (user.getEmail()!=null && user1.getEmail()==null){
            score=score+1;
        }
        if (user.getIntroduction()!=null && user1.getIntroduction()==null){

            score=score+1;
        }
        if ("1".equals(vipType)){//普通会员
            score = score*2;
        }else if("2".equals(vipType)){//超级会员
            score= score*3;
        }else {//普通用户
            score=score;
        }
        if(user1.getScore()!=null){
            user1.setScore(Integer.parseInt(user1.getScore())+score+"");
        }else{
            user1.setScore(score+"");

        }
        ScoreOrder scoreOrder = new ScoreOrder();
        scoreOrder.setScoreId(UuidUtils.getNumberId(10));
        scoreOrder.setTradeTime(date);
        scoreOrder.setGetType("1");//获取或消费渠道 1个人信息完善 2签到 3评论
        scoreOrder.setPlusOrMinus("1");//正负 0负 1正
        scoreOrder.setUserId(user1.getUserId());
        scoreOrder.setAmount(score+"");//积分数量
        user1.setExaminationState("0");
        user1.setUserId(user.getUserId());
        user1.setSex(user.getSex());
        user1.setBirthday(user.getBirthday());
        user1.setIdploma(user.getIdploma());
        user1.setGraduateSchool(user.getGraduateSchool());
        user1.setWorkExperienc(user.getWorkExperienc());
        user1.setEmail(user.getEmail());
        user1.setProvinceName(user.getProvinceName());
        user1.setIntroduction(user.getIntroduction());
        user1.setEmergencyPhone(user.getEmergencyPhone());
        user1.setEmergencyContact(user.getEmergencyContact());
        user1.setAddress(user.getAddress());
        int i =  userMapper.updateByPrimaryKeySelective(user1);
        scoreOrderMapper.insertSelective(scoreOrder);
        if (i>0){
            rspMsg.setOK();
            rspMsg.setMsg("保存成功，积分+"+score);
        }else{
            rspMsg.setError();
            rspMsg.setMsg("保存失败");
        }

        return rspMsg;
    }


    /**
     * 家长登录，查看老师对自己对的评论
     * @param taskReleaseId
     * @return
     */
    @PostMapping("/user/selectParentUserReviewsByIdUser")
    @ResponseBody
    public RspMsg<UserReviews> selectParentUserReviewsByIdUser(String taskReleaseId ) {
        RspMsg rspMsg = new RspMsg();
        Taskrelease taskrelease = taskreleaseMapper.selectByPrimaryKey(new Taskrelease().setTaskReleaseId(taskReleaseId));
        Example example = new Example(UserReviews.class);
        example.or().andEqualTo("taskReleaseId",taskReleaseId).andEqualTo("reviewId",taskrelease.getUserId()).andEqualTo("role","02");
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);
        if (userReviewsList.size()>0){
            UserReviews userReviews = userReviewsList.get(0);
            userReviews.setAvg(String.valueOf(Double.parseDouble(userReviews.getAvg())));
            rspMsg.setOK();
            rspMsg.setResult(userReviews);
        }else {
            rspMsg.setError();
            rspMsg.setMsg("未得到评论");
        }
        return  rspMsg;
    }


    /**
     * 老师登录，查看家长对自己对的评论
     * @param taskReleaseId
     * @return
     */
    @PostMapping("/user/selectTeacherUserReviewsByIdUser")
    @ResponseBody
    public RspMsg<UserReviews> selectTeacherUserReviewsByIdUser(String taskReleaseId ) {
        RspMsg rspMsg = new RspMsg();
        Taskrelease taskrelease = taskreleaseMapper.selectByPrimaryKey(new Taskrelease().setTaskReleaseId(taskReleaseId));
        Example example = new Example(UserReviews.class);
        example.or().andEqualTo("taskReleaseId",taskReleaseId).andEqualTo("reviewId",taskrelease.getRemark2()).andEqualTo("role","01");
        List<UserReviews> userReviewsList = userReviewsMapper.selectByExample(example);
        if (userReviewsList.size()>0){
            UserReviews userReviews = userReviewsList.get(0);
            userReviews.setAvg(String.valueOf(Double.parseDouble(userReviews.getAvg())));
            rspMsg.setOK();
            rspMsg.setResult(userReviews);
        }else {
            rspMsg.setError();
            rspMsg.setMsg("未得到评论");
        }
        return  rspMsg;
    }



}
